package com.ss.android.article.base.feature.detail2.e;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.bytedance.frameworks.base.mvp.Presenter;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.detail.model.ArticleDetail;
import com.ss.android.article.base.feature.detail.model.ArticleInfo;
import com.ss.android.article.base.feature.detail2.article.ad;
import com.ss.android.article.base.feature.detail2.d.DetailLoadResultHandler;
import com.ss.android.article.base.feature.detail2.d.NewDetailContext;
import com.ss.android.article.base.feature.detail2.e.a.CommonBizInteractor;
import com.ss.android.article.base.feature.detail2.e.a.NewDetailContextHolder;
import com.ss.android.article.base.feature.detail2.view.DetailView;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.article.base.utils.JSONObjectWrapper;
import com.ss.android.common.util.NetworkUtils;

import org.json.JSONObject;

import java.util.Iterator;

public final class NewDetailPresenter extends Presenter<com.ss.android.article.base.feature.detail2.view.f> {
    public AppData mAppData;
    public NewDetailContext mNewDetailContext;
    public DetailLoadResultHandler mDetailLoadResultHandler;
    public NewDetailContextHolder mNewDetailContextHolder;
    public CommonBizInteractor mCommonBizInteractor;
    public DetailLoadResultHandler.ResultListener<Article, ArticleDetail> mLoadArticleDetailResultListener = new DetailLoadResultHandler.ResultListener<Article, ArticleDetail>() {

        public final void onResult(Article article, ArticleDetail articleDetail) {
            if (articleDetail == null || !articleDetail.delete) {
                if (NewDetailPresenter.this.hasView()) {
                    NewDetailPresenter.this.mView.showContent(false);
                }
            } else if (NewDetailPresenter.this.hasView()) {
                NewDetailPresenter.this.mView.switchToArticleDeleted(article);
            }
        }
    };

    public NewDetailPresenter(Context context) {
        super(context);
        this.mAppData = AppData.getInstance();
        this.mNewDetailContext = new NewDetailContext();
        this.mNewDetailContextHolder = new NewDetailContextHolder(this.mContext, this.mNewDetailContext);
        this.mCommonBizInteractor = new CommonBizInteractor(this.mContext, this.mNewDetailContext);
        this.addInteractor(this.mNewDetailContextHolder);
        this.addInteractor(this.mCommonBizInteractor);
    }

    public final void share(int platform, Article article) {
        if (this.mCommonBizInteractor.mArticleShareHelper != null) {
            this.mCommonBizInteractor.mArticleShareHelper.share(platform, article);
        }
    }

    public final void appendContextParams(Intent intent) {
        NewDetailContext newDetailContext = this.mNewDetailContext;
        if (intent != null) {
            intent.putExtra("ad_id", newDetailContext.adId);
            intent.putExtra("bundle_download_app_extra", newDetailContext.logExtra);
            intent.putExtra("bundle_forbid_jump", newDetailContext.bundleForbidJump);
            intent.putExtra("group_id", newDetailContext.group_id);
            intent.putExtra("item_id", newDetailContext.item_id);
        }
    }

    public final void onCreate(Bundle bundle, Bundle arg5) {
        if (this.mNewDetailContext.a(bundle)) {
            super.onCreate(bundle, arg5);
            this.mDetailLoadResultHandler = new DetailLoadResultHandler(this.mContext, this.mNewDetailContext);
        } else if (this.hasView()) {
            this.mView.onCreate();
            this.mView.finish();
        }
    }

    public final void setSharePosition(String position) {
        if (!android.support.design.a.isTextEmpty(position)) {
            mCommonBizInteractor.mArticleShareHelper.position = position;
        }
    }

    public final void onEvent(String event, String position) {
        if (!android.support.design.a.isTextEmpty(event)) {
            JSONObjectWrapper jsonObjectWrapper = new JSONObjectWrapper();
            String enterFrom = this.mNewDetailContextHolder.mNewDetailContext.getEnterFrom();
            String categoryName = this.mNewDetailContextHolder.mNewDetailContext.categoryName;
            if (enterFrom != null) {
                if (enterFrom.equals("click_" + this.mNewDetailContextHolder.mNewDetailContext.categoryName)) {
                    enterFrom = "click_category";
                    categoryName = this.mNewDetailContextHolder.mNewDetailContext.categoryName;
                } else if (!enterFrom.equals("click_headline")) {
                    categoryName = enterFrom.replaceFirst("click_", "");
                }
            }

            long userId = 0;
            if (this.mNewDetailContextHolder.mNewDetailContext.article != null) {
                userId = this.mNewDetailContextHolder.mNewDetailContext.article.getUserId();
            }

            jsonObjectWrapper.put("enter_from", enterFrom)
                    .put("category_name", categoryName)
                    .put("user_id", userId)
                    .put("position", position)
                    .put("share_platform", null)
                    .put("group_id", this.mNewDetailContextHolder.mNewDetailContext.group_id)
                    .put("item_id", this.mNewDetailContextHolder.mNewDetailContext.item_id)
                    .put("log_pb", this.mNewDetailContextHolder.mNewDetailContext.logPb);
            if (AppData.getInstance().getAbSettings().isApplogStaging()) {
                jsonObjectWrapper.put("_staging_flag", 1);
            }

            android.support.design.a.onEvent(event, jsonObjectWrapper.jsonObject);
        }
    }

    public final void appendShareParams(JSONObject jsonObject) {
        if (jsonObject != null) {
            mCommonBizInteractor.mArticleShareHelper.fillShareParams(jsonObject);
        }
    }

    public final void onTopMoreTitleClick(boolean arg7) {
        if (mCommonBizInteractor.mArticleShareHelper != null && mCommonBizInteractor.mNewDetailContext.article != null && mCommonBizInteractor.mView != null) {
            mCommonBizInteractor.mArticleShareHelper.v = arg7;
            ArticleInfo v2 = ((DetailView) mCommonBizInteractor.mView).getArticleInfo();
            if (arg7) {
                switch (((DetailView) mCommonBizInteractor.mView).getCommentMode()) {
                    case 1:
                    case 2: {
                        mCommonBizInteractor.mArticleShareHelper.mCategoryName = mCommonBizInteractor.mNewDetailContext.categoryName;
                        mCommonBizInteractor.mArticleShareHelper.c(mCommonBizInteractor.mNewDetailContext.article, v2, mCommonBizInteractor.mNewDetailContext.adId);
                        return;
                    }
                    default: {
                        if (!mCommonBizInteractor.mNewDetailContext.article.m() && !mCommonBizInteractor.mNewDetailContext.article.n()) {
                            mCommonBizInteractor.mArticleShareHelper.b(mCommonBizInteractor.mNewDetailContext.article, v2, mCommonBizInteractor.mNewDetailContext.adId);
                            return;
                        }

                        mCommonBizInteractor.mArticleShareHelper.a(mCommonBizInteractor.mNewDetailContext.article, v2, mCommonBizInteractor.mNewDetailContext.adId);
                        return;
                    }
                }
            } else {
                mCommonBizInteractor.mArticleShareHelper.mCategoryName = mCommonBizInteractor.mNewDetailContext.categoryName;
                mCommonBizInteractor.mArticleShareHelper.l = v2;
                mCommonBizInteractor.mArticleShareHelper.a(mCommonBizInteractor.mNewDetailContext.article, mCommonBizInteractor.mNewDetailContext.adId, true);
                return;
            }
        }
    }

    public final void onResume() {
        super.onResume();
        if (this.mDetailLoadResultHandler != null) {
            this.mDetailLoadResultHandler.mDetailLoader.onResume();
        }
    }

    public final void onPause() {
        super.onPause();
        if (this.mDetailLoadResultHandler != null) {
            this.mDetailLoadResultHandler.mDetailLoader.onPause();
        }
    }

    public final void onDestroy() {
        super.onDestroy();
        if (this.mDetailLoadResultHandler != null) {
            this.mDetailLoadResultHandler.mDetailLoader.onDestroy();
        }
    }

    public final void f() {
        if (this.mNewDetailContext.article == null || !this.mNewDetailContext.isVideo()) {
            if ((!this.mNewDetailContext.hasArticleUrl())
                    || this.mNewDetailContext.isVideo() || android.support.design.a.isTextEmpty(this.mNewDetailContext.article.articleUrl)) {
                if (this.hasView()) {
                    this.mView.showDetailErrorView();
                }

                if (this.mNewDetailContext.viewSingleId) {
                    this.mDetailLoadResultHandler.loadArticleDetail(Article.a(this.mNewDetailContext.group_id, this.mNewDetailContext.item_id), null, new Article(this.mNewDetailContext.group_id, this.mNewDetailContext.item_id, this.mNewDetailContext.aggrType), true, this.mLoadArticleDetailResultListener);
                    return;
                }

                this.mDetailLoadResultHandler.loadArticleDetail(this.mNewDetailContext.article.getItemKey(), this.mNewDetailContext.article, this.mNewDetailContext.article, true, this.mLoadArticleDetailResultListener);
            } else {
                if (NetworkUtils.isNetworkAvailable(this.mContext)) {
                    if (this.hasView()) {
                        this.mView.showContent(true);
                    }
                } else if (this.hasView()) {
                    this.mView.m();
                }

                return;
            }

        } else if (this.hasView()) {
            this.mView.showContent(true);
        }
    }

    public final void buildArticleUrl() {
        if (mNewDetailContext.article != null) {
            String articleUrl = mNewDetailContext.article.articleUrl;
            if (mNewDetailContext.extraSchemaUri != null && !TextUtils.isEmpty(articleUrl)) {
                Uri uri = Uri.parse(articleUrl);
                if (ad.a().contains(uri.getHost())) {
                    Uri.Builder builder = uri.buildUpon();
                    Iterator<String> iterator = mNewDetailContext.extraSchemaUri.getQueryParameterNames().iterator();
                    while (iterator.hasNext()) {
                        String parameter = iterator.next();
                        builder.appendQueryParameter(parameter, mNewDetailContext.extraSchemaUri.getQueryParameter(parameter));
                    }
                    articleUrl = builder.build().toString();
                }
            }

            mNewDetailContext.article.articleUrl = articleUrl;
            int v0_3 = (mNewDetailContext.groupFlags & 131072) <= 0 || mNewDetailContext.articleType != 1 ? 0 : 1;
            if (v0_3 == 0) {
                return;
            }

            articleUrl = mNewDetailContext.article.articleUrl;
            if (!TextUtils.isEmpty(articleUrl) && (this.isGalleryFlat())) {
                Uri.Builder builder = Uri.parse(articleUrl).buildUpon();
                builder.encodedFragment("support_gallery=false");
                articleUrl = builder.toString();
            }
            mNewDetailContext.article.articleUrl = articleUrl;
        }
    }

    public final boolean isGalleryFlat() {
        return this.mAppData.getAbSettings().isGalleryFlat();
    }
}

